package com.weijian.dal.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.weijian.dal.entity.CartItem;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 购物车数据访问层
 *
 * @author weijian
 */
@Mapper
public interface CartItemMapper extends BaseMapper<CartItem> {

    /**
     * 查询用户的购物车商品列表
     *
     * @param userId 用户ID
     * @return 购物车商品列表
     */
    @Select("SELECT * FROM cart_item WHERE user_id = #{userId} AND deleted = 0 ORDER BY update_time DESC")
    List<CartItem> findByUserId(@Param("userId") Long userId);

    /**
     * 根据用户ID和课程ID查询购物车商品
     *
     * @param userId   用户ID
     * @param courseId 课程ID
     * @return 购物车商品
     */
    @Select("SELECT * FROM cart_item WHERE user_id = #{userId} AND course_id = #{courseId} AND deleted = 0")
    CartItem findByUserIdAndCourseId(@Param("userId") Long userId, @Param("courseId") Long courseId);

    /**
     * 查询用户选中的购物车商品
     *
     * @param userId 用户ID
     * @return 选中的购物车商品列表
     */
    @Select("SELECT * FROM cart_item WHERE user_id = #{userId} AND selected = 1 AND deleted = 0 ORDER BY update_time DESC")
    List<CartItem> findSelectedByUserId(@Param("userId") Long userId);

    /**
     * 统计用户购物车商品数量
     *
     * @param userId 用户ID
     * @return 商品数量
     */
    @Select("SELECT COUNT(*) FROM cart_item WHERE user_id = #{userId} AND deleted = 0")
    int countByUserId(@Param("userId") Long userId);
}